#include "MetallAIComponent.h"
#include "GameObject.h"
#include "..\Managers\GameObjectManager.h"
#include "..\Factory\EnemyProjectileFactory.h"
#include "..\Components\VisualComponent.h"
#include "..\Components\AnimationComponent.h"

#include <iostream>
#include <math.h>

MetallAIComponent::MetallAIComponent(const std::string& p_target) : m_target(p_target), m_bulletVelocity(2.0f), m_isShooting(false), m_timer(0.0f), m_delayShoot(0.0f), m_activateDistance(10.0f), m_aiState(AI_METALL_IDLE)
{
	m_aiFuncPtr[AI_METALL_IDLE] = &MetallAIComponent::idle;
	m_aiFuncPtr[AI_METALL_ATTACKING] = &MetallAIComponent::attacking;
	m_aiFuncPtr[AI_METALL_POP_UP] = &MetallAIComponent::popUp;
}

MetallAIComponent::~MetallAIComponent()
{
	delete m_projectileFactory;
}

void MetallAIComponent::init()
{
	m_projectileFactory = new EnemyProjectileFactory;

	IComponent* ic = getOwner()->getComponent("VisualComponent");
	m_visual = static_cast<VisualComponent*>(ic);
	ic = getOwner()->getComponent("AnimationComponent");
	m_animations = static_cast<AnimationComponent*>(ic);
	ic = getOwner()->getComponent("CollisionComponent");
	m_animations->setAnimation("idle");
}

void MetallAIComponent::update(const GameTime& p_gameTime)
{
	(this->*m_aiFuncPtr[m_aiState])(p_gameTime);
}

void MetallAIComponent::idle(const GameTime& p_gameTime)
{
	
	GameObject* go = getOwner()->getGameObjectManager()->getGameObject(m_target);
	float distance =  sqrt(go->getGameObjectManager()->getGameObject("player")->getPosition().x - go->getGameObjectManager()->getGameObject("metall")->getPosition().x);

	if(distance < m_activateDistance)
	{
		m_animations->setAnimation("loop");
		m_aiState = AI_METALL_POP_UP;
	}
}
	
void MetallAIComponent::attacking(const GameTime& p_gameTime)
{
	m_timer = std::min(m_timer + p_gameTime.deltaTime(), 1.2f);
	if(m_timer >= 1.2f)
	{
		m_animations->setAnimation("idle");
		m_aiState = AI_METALL_IDLE;
	}
}

void MetallAIComponent::popUp(const GameTime& p_gameTime)
{
	m_timer = std::min(m_timer + p_gameTime.deltaTime(), 1.2f);
	if(m_timer >= 1.2f)
	{
		m_timer = 0.0f;
		m_aiState = AI_METALL_ATTACKING;

		GameObject* go = m_projectileFactory->createProjectile(vn::Vector2(m_visual->getSprite()->getPosition()), vn::Vector2(m_bulletVelocity * m_visual->getSprite()->getScale().x, 2.0f));

				getOwner()->getGameObjectManager()->addGameObject(go);
				go->setGameObjectManager(go->getGameObjectManager());
				
				go = m_projectileFactory->createProjectile(vn::Vector2(m_visual->getSprite()->getPosition()), vn::Vector2(m_bulletVelocity * m_visual->getSprite()->getScale				().x, 0.0f));

				getOwner()->getGameObjectManager()->addGameObject(go);
				go->setGameObjectManager(go->getGameObjectManager());
				
				go = m_projectileFactory->createProjectile(vn::Vector2(m_visual->getSprite()->getPosition()), vn::Vector2(m_bulletVelocity * m_visual->getSprite()->getScale				().x, -2.0f));

				getOwner()->getGameObjectManager()->addGameObject(go);
				go->setGameObjectManager(go->getGameObjectManager());
	}
}